package com.pj.project.sys_client_visit;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.pj.project4sp.SP;
import com.pj.utils.sg.AjaxJson;
import com.pj.utils.so.SoMap;

import cn.dev33.satoken.annotation.SaCheckPermission;


/**
 * Controller: sys_client_visit -- 应用访问关系表
 * @author click33 
 */
@RestController
@RequestMapping("/SysClientVisit/")
public class SysClientVisitController {

	/** 底层 Mapper 对象 */
	@Autowired
	SysClientVisitMapper sysClientVisitMapper;

	/** 增 */  
	@RequestMapping("add")
	@SaCheckPermission(SysClientVisit.PERMISSION_CODE)
	@Transactional(rollbackFor = Exception.class)
	public AjaxJson add(SysClientVisit s){
		// 校验数据完整性
		SysClientVisitUtil.check(s);
		// 如果对应的关系已经有了，则先删除已有的，防止重复 
		SysClientVisit s2 = sysClientVisitMapper.getByClientAndUserId(s.getClientId(), s.getUserId());
		if(s2 != null) {
			sysClientVisitMapper.deleteByClientAndUserId(s.getClientId(), s.getUserId());
		}
		// 开始新增 
		sysClientVisitMapper.add(s);
		s = sysClientVisitMapper.getById(SP.publicMapper.getPrimarykey());
		return AjaxJson.getSuccessData(s);
	}

	/** 删 */  
	@RequestMapping("delete")
	@SaCheckPermission(SysClientVisit.PERMISSION_CODE)
	public AjaxJson delete(Long id){
		int line = sysClientVisitMapper.delete(id);
		return AjaxJson.getByLine(line);
	}
	
	/** 删 - 根据id列表 */  
	@RequestMapping("deleteByIds")
	@SaCheckPermission(SysClientVisit.PERMISSION_CODE)
	public AjaxJson deleteByIds(){
		List<Long> ids = SoMap.getRequestSoMap().getListByComma("ids", long.class); 
		int line = SP.publicMapper.deleteByIds(SysClientVisit.TABLE_NAME, ids);
		return AjaxJson.getByLine(line);
	}
	
	/** 改 */  
	@RequestMapping("update")
	@SaCheckPermission(SysClientVisit.PERMISSION_CODE)
	public AjaxJson update(SysClientVisit s){
		// 校验数据完整性
		SysClientVisitUtil.check(s);
		// 如果对应的关系已经有了，则先删除已有的，防止重复 
		SysClientVisit s2 = sysClientVisitMapper.getByClientAndUserId(s.getClientId(), s.getUserId());
		if(s2 != null && (long)s2.getId() != s.getId()) {
			sysClientVisitMapper.deleteByClientAndUserId(s.getClientId(), s.getUserId());
		}
		// 开始修改 
		int line = sysClientVisitMapper.update(s);
		return AjaxJson.getByLine(line);
	}

	/** 查 - 根据id */  
	@RequestMapping("getById")
	@SaCheckPermission(SysClientVisit.PERMISSION_CODE)
	public AjaxJson getById(Long id){
		SysClientVisit s = sysClientVisitMapper.getById(id);
		return AjaxJson.getSuccessData(s);
	}

	/** 查集合 - 根据条件（参数为空时代表忽略指定条件） */  
	@RequestMapping("getList")
	@SaCheckPermission(SysClientVisit.PERMISSION_CODE)
	public AjaxJson getList() { 
		SoMap so = SoMap.getRequestSoMap();
		List<SysClientVisit> list = sysClientVisitMapper.getList(so.startPage());
		return AjaxJson.getPageData(so.getDataCount(), list);
	}
	
}
